#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "testutils.h"
#include "interface.h"

void play_full_game(struct gameState *state, int kcards[], int num_players, int game_is_good, FILE *game_fp, FILE *result_fp)
{
	assert(game_fp != NULL);
	assert(result_fp != NULL);

	int i;
	int turn_num = 0;
	int players[MAX_PLAYERS];
	int current_player;

	while (TRUE) {
		if (isGameOver(state)) {
			printf("\n\n*****************\n****GAME OVER****\n*****************\n\n");
			break;
		}

		current_player = whoseTurn(state);
		executeBotTurn(current_player, &turn_num, state, kcards, game_fp, result_fp);
		printState(state, game_fp);
	}

	printScores(state, game_fp);
	getWinners(players, state);

	printf("After %d turns, the winner(s) are:\n", turn_num);
	for (i = 0; i < state->numPlayers; i++) {
		if (players[i] == WINNER) {
			printf("Player %d\n", i);
		}
	}
}

void test_dominion(void)
{
	int num_players;
	int seed = 1000;
	struct gameState state;
	int k[10];

	FILE *game_fp = fopen("gameResults.out", "w");
	FILE *result_fp = fopen("testdominion.out", "w");

	srand(time(NULL));

	selectKingdomCards(seed, k);

	num_players = rand() % 3 + 2;

	int game_is_good = initializeGame(num_players, k, seed, &state);

	if (game_is_good == 0) {
		play_full_game(&state, k, num_players, game_is_good, game_fp, result_fp);
	}
	else {
		printf("Error initializating game. Exiting.\n");
	}

	fclose(game_fp);
	fclose(result_fp);
}

int main(void)
{
	test_dominion();
	return 0;
}